package com.zego.wawaji.ui.center.transport;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.text.util.Linkify;
import android.view.View;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.utils.ChineseFilter;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.base.stepview.StepView;
import com.zego.wawaji.ui.center.transport.bean.StepItemData;
import com.zego.wawaji.ui.center.transport.bean.StepList;

import java.util.regex.Pattern;

/**
 * 我的娃娃：物流信息
 * Created by Su on 2017/11/26.
 */
public class WawaLogisticsAct extends BaseAppCompatActivity {

    private StepView stepView;
    private StepList stepList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_wawa_logistics_act);
        initView();
    }

    private void initView() {
        setBackView();
        setTitle("查看物流");
        stepList = getIntent().getParcelableExtra("stepList");
        if (stepList == null) {
            finish();
            return;
        }

        TextView mailType = (TextView) findViewById(R.id.mail_type);
        TextView mailNo = (TextView) findViewById(R.id.mail_no);
        mailType.setText(stepList.getExpTextName());
        mailNo.setText(stepList.getMailNo());
        stepView = (StepView) findViewById(R.id.stepView);
        stepView.setDatas(stepList.getStepList());
        stepView.setBindViewListener((itemMsg, itemDate, data) -> {
            StepItemData sid = (StepItemData) data;
            itemMsg.setText(formatPhoneNumber(itemMsg, sid.getMsg()));
            itemDate.setText(sid.getDate());
        });
    }

    /**
     * 格式化TextView的显示格式，识别手机号
     */
    private SpannableStringBuilder formatPhoneNumber(TextView textView, String source) {
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        SpannableStringBuilder value = new SpannableStringBuilder(source);
        // 使用正则表达式匹配电话
        Linkify.addLinks(value, Pattern.compile(ChineseFilter.PATTERN_PHONE_DETAIL), ChineseFilter.SCHEME_TEL);

        // 获取上面到所有 addLinks 后的匹配部分(这里一个匹配项被封装成了一个 URLSpan 对象)
        URLSpan[] urlSpans = value.getSpans(0, value.length(), URLSpan.class);
        for (final URLSpan urlSpan : urlSpans) {
            if (urlSpan.getURL().startsWith(ChineseFilter.SCHEME_TEL)) {
                int start = value.getSpanStart(urlSpan);
                int end = value.getSpanEnd(urlSpan);
                value.removeSpan(urlSpan);
                value.setSpan(new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        final String phone = urlSpan.getURL().replace(ChineseFilter.SCHEME_TEL, "");
                        AlertDialog.Builder builder = new AlertDialog.Builder(WawaLogisticsAct.this);
                        builder.setMessage("是否拨打电话：" + phone);
                        builder.setNegativeButton("取消", null);
                        builder.setPositiveButton("确定", (dialog, which) -> {
                            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                            startActivity(intent);
                        });
                        builder.create().show();
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        super.updateDrawState(ds);
                        ds.setColor(Color.parseColor("#3f8de2"));
                        ds.setUnderlineText(true);
                    }
                }, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        return value;
    }
}
